﻿using Cart.SanPhamSrv;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cart
{
    public partial class PopupAnhCT : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IdSanPham"] != null)
            {
                using (var spService = new SanPhamSrv.SanPhamServiceClient())
                {
                    var modelsanpham = spService.ChiTietSanPham(Convert.ToInt64(Session["IdSanPham"]));
                    lbLoai.InnerText = modelsanpham.Ten.ToUpper();
                }
            }
        }

        protected void register_button1_Click(object sender, EventArgs e)
        {
            using (var spService = new SanPhamSrv.SanPhamServiceClient())
            {
                if (fuAnh.HasFile && Session["IdSanPham"] != null)
                {
                    var spct = new SanPhamAnhCT();
                    spct.IdSanPham = Convert.ToInt64(Session["IdSanPham"]);
                    spct.LinkAnh = fuAnh.FileName;
                    string theFileName = Path.Combine(Server.MapPath("~/chitiet_files"), fuAnh.FileName);
                    if (File.Exists(theFileName))
                    {
                        File.Delete(theFileName);
                    }
                    fuAnh.SaveAs(theFileName);
                    var result = spService.CapNhatSanPhamChiTiet(spct, null);
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + result + "');</script>");
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.close();if (window.opener && !window.opener.closed) {window.opener.location.reload();}", true);
                }
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Không lấy được dữ liệu sản phẩm');</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.close();", true);
            }
        }
    }
}